package com.apes.express;

import com.apes.framework.plugin.fsm.schedule.WorkSchedule;
import com.apes.framework.plugin.fsm.schedule.WorkScheduleItem;
import com.apes.framework.util.DateUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.apes.framework.util.DateUtil.SHOW_DATETIME_FORMAT;

public class ExpressRunnerSample {

    public static void main(String[] args) throws Exception {
        WorkSchedule ws = new WorkSchedule();
        ws.setName("门店");
        List<WorkScheduleItem> scheduleItems = new ArrayList<>();
        WorkScheduleItem wsi = new WorkScheduleItem();
        wsi.setStart("9:00");
        wsi.setEnd("12:00");
        scheduleItems.add(wsi);
        wsi = new WorkScheduleItem();
        wsi.setStart("13:00");
        wsi.setEnd("18:00");
        scheduleItems.add(wsi);
        ws.setWorkScheduleItems(scheduleItems);
        System.out.println(ws.getDescription());

        Date t = DateUtil.parse("2021-01-09 12:15:30", SHOW_DATETIME_FORMAT);

        t = ws.calcExpired(t, 3 * 60 + 30);

        String o = DateUtil.format(t, SHOW_DATETIME_FORMAT);
        System.out.println(o);

//        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
//        Date date1 = sdf.parse("14:23");
//        Date date2 = sdf.parse("09:25");
//
//        System.out.println("date1 : " + sdf.format(date1));
//        System.out.println("date2 : " + sdf.format(date2));
//
//        System.out.println( DateUtil.format(new Date(), "HH:mm"));
//
//        if (date1.after(date2)) {
//            System.out.println("Date1 时间在 Date2 之后");
//            long i = (date1.getTime() - date2.getTime()) / 1000;
//            long day1 = i / (24 * 3600);
//            long hour1 = i % (24 * 3600) / 3600;
//            long minute1 = i % 3600 / 60;
//            long second1 = i % 60 / 60;
//            System.out.println("" + day1 + "天" + hour1 + "小时" + minute1 + "分" + second1 + "秒");
//        }
//
//        if (date1.before(date2)) {
//            System.out.println("Date1 时间在 Date2 之前");
//        }
//
//        if (date1.equals(date2)) {
//            System.out.println("Date1 时间与 Date2 相等");
//        }


//        ExpressRunner runner = new ExpressRunner(true, true);
//
//        String express = " 平均分 = (语文+数学+英语+综合考试)/4.0;return 平均分";
//        String[] names = FsmUtil.getOutVarNames(express);
//        for (String s : names) {
//            System.out.println("var : " + s);
//        }

//        /**编译脚本，查询外部需要定义的变量和函数**/
//        String[] names = runner.getOutVarNames(express);
//        for (String s : names) {
//            System.out.println("var : " + s);
//        }


        /**macro 宏定义**/
//        runner.addMacro("平均成绩", "(语文+数学+英语)/3.0");
//        runner.addMacro("是否优秀", "平均成绩>90");
//        IExpressContext<String, Object> context = new DefaultContext<>();
//        context.put("语文", 88);
//        context.put("数学", 99);
//        context.put("英语", 95);
//        Object result = runner.execute("是否优秀", context, null, false, false);
//        System.out.println(result);


//        /**绑定java类或者对象的method**/
//        runner.addFunctionOfClassMethod("计算时间", FsmUtil.class.getName(), "calcTime",
//                new String[]{"java.util.Date", "String"}, null);
//        runner.addFunctionOfServiceMethod("打印", System.out, "println", new String[]{"String"}, null);
//        String exp = "取绝对值(-100); 打印(\"你好吗？\");";
//        runner.execute(exp, null, null, false, false);

//        /**语法解析校验：如果调用过程不出现异常，指令集instructionSet就是可以被加载运行（execute）**/
//        exp = "for(i=0;i<10;i++){sum=i+1}return sum;";
//        InstructionSet instructionSet = runner.parseInstructionSet(exp);
//        System.out.println();

//        express = "计算时间(接车时间, '每日22点之后')";
//        Map context = new HashMap();
//        context.put("接车时间", new Date());
//        Object result = FsmUtil.execute(express, context);
//        System.out.println(result);
    }
}
